package logic

import (
	"context"
	"group/cache"
	"time"

	"github.com/mojocn/base64Captcha"
	"github.com/zeromicro/go-zero/core/logx"
	"group/grouprpc/grouprpc"
	"group/grouprpc/internal/svc"
)

type CaptchaCodeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCaptchaCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CaptchaCodeLogic {
	return &CaptchaCodeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CaptchaCodeLogic) CaptchaCode(in *grouprpc.CaptchaCodeRequest) (*grouprpc.CaptchaCodeResponse, error) {
	// todo: add your logic here and delete this line
	c := base64Captcha.NewCaptcha(base64Captcha.DefaultDriverDigit, base64Captcha.DefaultMemStore)
	_, s, s2, _ := c.Generate()
	cache.Rdb.Set(cache.Ctx, "captcha"+in.Account, s2, time.Minute*5)
	return &grouprpc.CaptchaCodeResponse{
		CaptchaImage: s,
	}, nil
}
